package high.concurrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import high.concurrent.dao.BannerMapper;
import high.concurrent.model.BannerDO;
import high.concurrent.service.BannerService;
import high.concurrent.utils.JsonData;
import high.concurrent.vo.BannerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BannerServiceImpl implements BannerService {


    @Autowired(required = false)
    private BannerMapper bannerMapper;
    /**
     * 轮播图列表查询
     * 轮播图不需要登录也可以进，是首页上方大轮播图，所以不需要拦截器
     * @return
     */
    @Override
    public JsonData selectBannerList() {

        // 查询轮播图列表
        List<BannerDO> listBannerDOs = bannerMapper.selectList(new QueryWrapper<BannerDO>()
                .orderByDesc("weight"));

        // 类型转换为VO, 按照权重倒序排列显示
        List<BannerVO> listBannerVOs = listBannerDOs.stream()
                .map(bannerDO -> bannerDoToVo(bannerDO))
                .collect(Collectors.toList());

        return JsonData.buildSuccess(listBannerVOs);
    }


    /**
     * 轮播图的DO 转成 VO
     * @param bannerDO  DO对象
     * @return
     */
    private BannerVO bannerDoToVo(BannerDO bannerDO){
        BannerVO bannerVO = new BannerVO();
        BeanUtils.copyProperties(bannerDO, bannerVO);
        return bannerVO;
    }
}
